/**
 * Copyright (c) 快宝网络 kuaidihelp.com Co., Ltd. All Rights Reserved 禁止外泄以及用于其它的商业用途
 */

import { message } from "antd";
import {
  getAccountList,
  getAccountInfo,
  rechargePay,
  rechargeCallBack,
  bindAccount,
  getSmsCode,
  getAccountDetail,
  getTypeOptions,
  getCash,
  getFeeDetail,
  exportDetail,
  transformToConsumer,
  editBalanceWarningInfo,
  getBalanceWarningInfo,
} from "@/services/account";
import { isArray, isLegalData } from "@/utils/utils";

export default {
  namespace: "account",
  state: {
    accountList: null,
    info: null,
    withdrawDetailList: {},
    availDetailList: {},
    typeSelectValue: null,
    balanceWarningInfo: {},
  },

  effects: {
    *getUserAccount(_, { select, put }) {
      const { parent_phone } = yield select(_ => _.user.currentUser);
      yield put({
        type: "save",
        payload: {
          phone: parent_phone,
        },
      });
    },
    *getAccountList({ payload }, { call, put }) {
      const { code, data } = yield call(getAccountList, payload);
      if (code == 0) {
        yield put({
          type: "save",
          payload: {
            accountList: isArray(data) ? data : null,
          },
        });
      }
    },
    *getAccountInfo(_, { call, put }) {
      const { code, data } = yield call(getAccountInfo);
      if (code == 0) {
        yield put({
          type: "save",
          payload: {
            info: data,
          },
        });
      }
    },
    *rechargePay({ payload, callback }, { call, put }) {
      const { code, data } = yield call(rechargePay, payload);
      if (code == 0) {
        callback(data.out_trade_no);
        data.qrcode_url = `http://m.kuaidihelp.com/order/wxqrcode?data=${
          data.qrcode_url
        }&logo=alipay`;
        yield put({
          type: "save",
          payload: {
            payInfo: data,
          },
        });
      }
    },
    *rechargeCallBack({ payload, callback }, { call }) {
      const { code, msg } = yield call(rechargeCallBack, payload);
      if (code == 0) {
        callback(0);
      } else {
        msg.includes("失败") && message.error(msg);
      }
    },
    *getCash({ payload, then }, { call }) {
      const { code, msg } = yield call(getCash, payload);
      if (code == 0) {
        then(msg);
      } else {
        message.error(msg);
      }
    },
    *getSmsCode({ payload, then }, { call }) {
      const { code, msg } = yield call(getSmsCode, payload);
      if (code == 0) {
        message.success("发送成功");
        then(0);
      } else {
        message.success(msg);
      }
    },
    *bindAccount({ payload, then }, { call, put }) {
      const { code, msg } = yield call(bindAccount, payload);
      // 更新绑定账户
      const { alipay_name, alipay_account } = payload || {};
      if (code == 0) {
        yield put({
          type: "updateInfo",
          payload: {
            alipay_name,
            alipay_account,
          },
        });
      }
      then(code, msg);
    },
    *getAccountDetail({ payload }, { call, put }) {
      const { code, data = {} } = yield call(getAccountDetail, payload);
      if (code == 0) {
        // withdraw | avail
        const { accountType } = payload;
        yield put({
          type: "save",
          payload: {
            [`${accountType}DetailList`]: {
              list: data.list,
              pagination: {
                total: parseInt(data.total),
                pageSize: parseInt(data.pageSize),
                current: parseInt(data.page),
              },
            },
          },
        });
      }
    },
    *getTypeOptions({ payload }, { call, put, select }) {
      const { accountType } = yield select(_ => _.account);
      if (accountType !== payload.accountType) {
        const { code, data } = yield call(getTypeOptions, payload);
        if (code == 0) {
          yield put({
            type: "save",
            payload: {
              typeSelectValue: data,
              accountType: payload.accountType,
            },
          });
        }
      }
    },
    // 共配费用明细，获取列表
    *getFeeDetail({ payload, __dva_resolve, __dva_reject }, { call }) {
      const response = yield call(getFeeDetail, payload);
      const { export: isExport } = payload;
      if (!response) return;
      const { code, data, msg } = response;
      if (code == 0) {
        if (isExport == 1) {
          message.success("导出成功");
        }
        __dva_resolve(isArray(data) ? data : []);
      } else {
        __dva_reject();
        message.error(msg);
      }
    },
    // 共配费用明细，明细导出
    *exportDetail({ payload, __dva_resolve, __dva_reject }, { call }) {
      const response = yield call(exportDetail, payload);
      if (!response) return;
      const { code, msg } = response;
      if (code == 0) {
        message.success("导出成功");
        __dva_resolve();
      } else {
        __dva_reject();
        message.error(msg);
      }
    },
    // 资金账户，账户余额，转入消费账户
    *transformToConsumer({ payload, __dva_resolve, __dva_reject }, { call }) {
      const response = yield call(transformToConsumer, payload);
      if (!response) return;
      const { code, msg } = response;
      if (code == 0) {
        message.success(msg);
        __dva_resolve();
      } else {
        __dva_reject();
        message.error(msg);
      }
    },
    // 资金账户，账户余额，修改余额预警
    *editBalanceWarningInfo({ payload, __dva_resolve, __dva_reject }, { call }) {
      const response = yield call(editBalanceWarningInfo, payload);
      if (!response) return;
      const { code, msg } = response;
      if (code == 0) {
        message.success(msg);
        __dva_resolve();
      } else {
        __dva_reject();
        message.error(msg);
      }
    },
    // 资金账户，账户余额，获取余额预警
    *getBalanceWarningInfo({ payload, __dva_resolve, __dva_reject }, { call, put }) {
      const response = yield call(getBalanceWarningInfo, payload);
      if (!response) return;
      const { code, data, msg } = response;
      if (code == 0) {
        yield put({
          type: "save",
          payload: {
            balanceWarningInfo: isLegalData(data, {}),
          },
        });
        __dva_resolve();
      } else {
        __dva_reject();
        message.error(msg);
      }
    },
  },

  reducers: {
    updateInfo(state, { payload }) {
      return {
        ...state,
        info: {
          ...state.info,
          ...payload,
        },
      };
    },
    save(state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    },
  },
};
